package com.dollar.juc.dynamicThreadPool;

import com.dollar.juc.dynamicThreadPool.alarm.AlarmConfig;
import com.dollar.juc.dynamicThreadPool.alarm.DynamicThreadPoolAlarm;
import com.dollar.juc.dynamicThreadPool.alarm.Owner;
import com.dollar.juc.dynamicThreadPool.alarm.ThreadPoolSmsAlarmNotify;
import com.dollar.juc.dynamicThreadPool.enums.RejectedExecutionHandlerEnum;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 * https://www.modb.pro/db/26927
 * https://tech.meituan.com/2020/04/02/java-pooling-pratice-in-meituan.html
 * https://mp.weixin.qq.com/s?__biz=Mzg3NjU3NTkwMQ==&mid=2247505103&idx=1&sn=a041dbec689cec4f1bbc99220baa7219&source=41#wechat_redirect
 */
public class Main {
    public static void main(String[] args) {

        Owner owner = new Owner("jack");
        AlarmConfig alarmConfig = new AlarmConfig(owner);
        DynamicThreadPoolManager dynamicThreadPoolManager = new DynamicThreadPoolManager(alarmConfig);
        ThreadPoolExecutor threadPoolExecutor = dynamicThreadPoolManager.createThreadPoolExecutor("io-pool", 2, 4, 60, TimeUnit.SECONDS,
                new ArrayBlockingQueue(10), RejectedExecutionHandlerEnum.ABORT_POLICY);
        DynamicThreadPoolAlarm dynamicThreadPoolAlarm = new DynamicThreadPoolAlarm(dynamicThreadPoolManager, new ThreadPoolSmsAlarmNotify(), false);
        dynamicThreadPoolAlarm.initAlarm();
        for (int i = 0; i < 50; i++) {
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            threadPoolExecutor.execute(() -> {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

    }
}
